package com.itheima;

import com.itheima.domain.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CrudDemo {
    private InputStream is =null;
    private SqlSession session =null;

    @Before
    public void before()throws Exception{
        //1、读取核心配置文件信息
        is = Resources.getResourceAsStream("sql.xml");
        //2、构建SqlSessionFactory对象(构建者模式)
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
        //3、获取SqlSession对象
        session = factory.openSession();
    }

    @After
    public void after()throws Exception{
        //5、事务提交
        session.commit();
        //6、释放资源
        is.close();
        session.close();
    }

    //1.统计
    @Test
    public void testTotal(){
        //4、调用statement对象
        //statement对象的标识的写法：namespace+"."+statement对象的id
        int i = session.selectOne("one.count");
        System.out.println(i);
    }

    //2.查询所有
    @Test
    public void testFindAll(){
        List<Student> list = session.selectList("one.findAll");
        System.out.println(list);
    }

    //3.根据id查询
    @Test
    public void testFindById(){
        Student student = session.selectOne("one.findById", "2");
        System.out.println(student);
    }

    //4、新增操作
    @Test
    public void testSave() {
        //1、组装数据
        Student stu = new Student();
        stu.setId(5);
        stu.setName("小小");
        stu.setAge(18);
        stu.setGender("女");
        stu.setScore(123);

        //2、调用statement对象
        session.insert("one.save",stu);
    }

    //5、根据id和ename查询
    @Test
    public void testFindByNameAndPwd() {
        //map集合存放用户名和密码
        Map map = new HashMap();
        map.put("username","李四");
        map.put("pwd","2");
        Student stu = session.selectOne("one.findByNameAndPwd", map);
        System.out.println(stu);
    }

    //6、更新操作
    @Test
    public void testUpdate(){
        //map集合存放用户名和密码
        Map map=new HashMap();
        map.put("name","小乔");
        map.put("id","5");

        session.update("one.update",map);
    }

    //7、根据id删除
    @Test
    public void testDelete(){
        session.delete("one.delete","5");
    }

    //8.1、模糊查询（占位符）
    @Test
    public void testFindLikeName1(){
        List<Student> list = session.selectList("one.findLikeName1", "%三%");
        System.out.println(list);
    }

    //8.2、模糊查询（字符串拼接）
    @Test
    public void testFindLikeName2(){
        List<Student> list = session.selectList("one.findLikeName2", "四");
        System.out.println(list);
    }

    //9、统计男女数量
    @Test
    public void testSexCount() {
        List<Map> list = session.selectList("one.sexCount");

        Map mapSex = new HashMap();
        for (Map map : list) {
            System.out.println(map);
        }
    }
}
